﻿Namespace B1Objects
    ''' <summary>
    ''' 重写了recordset附加一些方法
    ''' </summary>
    ''' <remarks></remarks>
    Friend Class Recordset
        Implements B1Objects.IRecordset
        Private _DataTable As Data.DataTable
        Private sapRecordSet As SAPbobsCOM.Recordset

        Public Sub New(ByVal b1Recordset As SAPbobsCOM.Recordset)
            sapRecordSet = b1Recordset
        End Sub


        Public ReadOnly Property BoF As Boolean Implements SAPbobsCOM.IRecordset.BoF
            Get
                Return sapRecordSet.BoF
            End Get
        End Property

        Public ReadOnly Property Command As SAPbobsCOM.Command Implements SAPbobsCOM.IRecordset.Command
            Get
                Return sapRecordSet.Command
            End Get
        End Property

        Public Sub DoQuery(ByVal QueryStr As String) Implements SAPbobsCOM.IRecordset.DoQuery
            sapRecordSet.DoQuery(QueryStr)
            _DataTable = Nothing
        End Sub

        Public ReadOnly Property EoF As Boolean Implements SAPbobsCOM.IRecordset.EoF
            Get
                Return sapRecordSet.EoF
            End Get
        End Property

        Public ReadOnly Property Fields As SAPbobsCOM.Fields Implements SAPbobsCOM.IRecordset.Fields
            Get
                Return sapRecordSet.Fields
            End Get
        End Property

        Public Function GetAsXML() As String Implements SAPbobsCOM.IRecordset.GetAsXML
            Return sapRecordSet.GetAsXML()
        End Function

        Public Sub MoveFirst() Implements SAPbobsCOM.IRecordset.MoveFirst
            sapRecordSet.MoveFirst()
        End Sub

        Public Sub MoveLast() Implements SAPbobsCOM.IRecordset.MoveLast
            sapRecordSet.MoveLast()
        End Sub

        Public Sub MoveNext() Implements SAPbobsCOM.IRecordset.MoveNext
            sapRecordSet.MoveNext()
        End Sub

        Public Sub MovePrevious() Implements SAPbobsCOM.IRecordset.MovePrevious
            sapRecordSet.MovePrevious()
        End Sub

        Public ReadOnly Property RecordCount As Integer Implements SAPbobsCOM.IRecordset.RecordCount
            Get
                Return sapRecordSet.RecordCount
            End Get
        End Property

        Public Sub SaveToFile(ByVal FileName As String) Implements SAPbobsCOM.IRecordset.SaveToFile
            sapRecordSet.SaveToFile(FileName)
        End Sub

        Public Sub SaveXML(ByRef FileName As String) Implements SAPbobsCOM.IRecordset.SaveXML
            sapRecordSet.SaveXML(FileName)
        End Sub

        Public Function toDataTable() As Data.DataTable
            Dim doc As System.Xml.XmlDocument
            Dim dtCol As System.Data.DataColumn
            Dim dtRow As System.Data.DataRow
            Dim rsField As SAPbobsCOM.Field
            Dim xlist As System.Xml.XmlNodeList
            Dim xe As System.Xml.XmlElement
            Dim dt As System.Data.DataTable
            Try
                dt = New Data.DataTable()
                '构建表字段
                For i As Integer = 0 To sapRecordSet.Fields.Count - 1 Step 1
                    rsField = sapRecordSet.Fields.Item(i)
                    dtCol = New System.Data.DataColumn
                    dtCol.ColumnName = rsField.Name
                    dtCol.DataType = System.Type.GetType(GetFieldDotNetTypeSign(rsField))
                    dt.Columns.Add(dtCol)
                Next

                '赋值
                If sapRecordSet.RecordCount > 0 Then
                    doc = New System.Xml.XmlDocument
                    doc.LoadXml(sapRecordSet.GetAsXML)
                    xlist = doc.GetElementsByTagName("row")
                    For i As Integer = 0 To xlist.Count - 1 Step 1
                        dtRow = dt.NewRow()
                        xe = xlist.Item(i)
                        For j As Integer = 0 To xe.ChildNodes.Count - 1 Step 1
                            If dt.Columns.Item(j).DataType.FullName = "System.DateTime" Then
                                If xe.ChildNodes(j).InnerText = "" Then
                                    dtRow(j) = DBNull.Value
                                Else
                                    dtRow(j) = DateTime.ParseExact(xe.ChildNodes(j).InnerText, "yyyyMMdd", Nothing)
                                End If

                            Else
                                dtRow(j) = xe.ChildNodes(j).InnerText
                            End If
                        Next
                        dt.Rows.Add(dtRow)
                    Next
                End If
                doc = Nothing
                Return dt
            Catch ex As Exception
                Throw New Exception("Create system.data.table faild!" & ex.Message)
            End Try
        End Function

        Private Function GetFieldDotNetTypeSign(ByVal rsField As SAPbobsCOM.Field) As String
            Try
                Select Case rsField.Type
                    Case SAPbobsCOM.BoFieldTypes.db_Alpha
                        Select Case rsField.SubType
                            Case SAPbobsCOM.BoFldSubTypes.st_None
                                Return "System.String"
                            Case SAPbobsCOM.BoFldSubTypes.st_Address
                                Return "System.String"
                            Case SAPbobsCOM.BoFldSubTypes.st_Phone
                                Return "System.String"
                            Case SAPbobsCOM.BoFldSubTypes.st_Image
                                Return "System.String"
                            Case Else
                                Return "System.String"
                        End Select
                    Case SAPbobsCOM.BoFieldTypes.db_Date
                        Select Case rsField.SubType
                            Case SAPbobsCOM.BoFldSubTypes.st_None
                                Return "System.DateTime"
                            Case SAPbobsCOM.BoFldSubTypes.st_Time
                                Return "System.Int32"
                            Case Else
                                Return "System.String"
                        End Select
                    Case SAPbobsCOM.BoFieldTypes.db_Memo
                        Select Case rsField.SubType
                            Case SAPbobsCOM.BoFldSubTypes.st_None
                                Return "System.String"
                            Case SAPbobsCOM.BoFldSubTypes.st_Link
                                Return "System.String"
                            Case Else
                                Return "System.String"
                        End Select
                    Case SAPbobsCOM.BoFieldTypes.db_Float
                        Select Case rsField.SubType
                            Case SAPbobsCOM.BoFldSubTypes.st_Measurement
                                Return "System.Decimal"
                            Case SAPbobsCOM.BoFldSubTypes.st_Percentage
                                Return "System.Decimal"
                            Case SAPbobsCOM.BoFldSubTypes.st_Price
                                Return "System.Decimal"
                            Case SAPbobsCOM.BoFldSubTypes.st_Quantity
                                Return "System.Decimal"
                            Case SAPbobsCOM.BoFldSubTypes.st_Rate
                                Return "System.Decimal"
                            Case Else
                                Return "System.Decimal"
                        End Select
                    Case SAPbobsCOM.BoFieldTypes.db_Numeric
                        Select Case rsField.SubType
                            Case SAPbobsCOM.BoFldSubTypes.st_None
                                Return "System.Int32"
                            Case Else
                                Return "System.Int32"
                        End Select
                End Select
                Return "System.String"
            Catch ex As Exception
                Return "System.String"
            End Try
        End Function

        Public ReadOnly Property DataTable As System.Data.DataTable Implements IRecordset.DataTable
            Get
                If _DataTable Is Nothing Then
                    _DataTable = toDataTable()
                End If
                Return _DataTable
            End Get
        End Property

        Public Function CreateDataTableReader() As System.Data.DataTableReader Implements IRecordset.CreateDataTableReader
            Return DataTable.CreateDataReader()
        End Function

#If DI_Ver88 Then
        Public Function GetFixedSchema() As String Implements SAPbobsCOM.IRecordset.GetFixedSchema
            Return sapRecordSet.GetFixedSchema()
        End Function

        Public Function GetFixedXML(ByVal xmlMode As SAPbobsCOM.RecordsetXMLModeEnum) As String Implements SAPbobsCOM.IRecordset.GetFixedXML
            Return sapRecordSet.GetFixedXML(xmlMode)
        End Function
#End If
    End Class
End Namespace
